package br.com.vacine;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import br.com.vacine.Entity.UsuarioEntity;
import br.com.vacine.dao.DB;

public class ListaCartaoActivity extends ActionBarActivity{
	
	private List<UsuarioEntity> list = null;
	protected static int idPosicao;
	protected static int ida;
	private ListView listView;
	private int tamanho = 0;
	protected static boolean auxiliar;
	private AlertDialog alerta;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lista_cartoes);

        carregarDados();
        
	}

	@Override
	public void onResume(){
		super.onRestart();
		carregarDados();
		tamanho = 0;
	}
	
	public void carregarDados(){
        
		listView = (ListView)findViewById(R.id.listaUsuarios);

		DB db = new DB(this);
        list = db.listUsuario();
        
        if(list.size() == tamanho){
        
        	alerta();
        	tamanho = 1;
        	
        }else{
        
        	int v = 0;
        	List<String> listaNome = new ArrayList<String>();
    
        	for (UsuarioEntity s : list) {
        	
        		String object = list.get(v).getNome();
        		listaNome.add(object); 
        		v++;
        	}

        	ArrayAdapter<String> array = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listaNome);
        	listView.setAdapter(array);
        
        	listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

	        		idPosicao = position;
	        		ida = list.get(position).getIdUsuario();
	        		
	        		Intent it = new Intent(getBaseContext(), TelaUsuarioActivity.class);
	        		startActivity(it);

	        		finish();
				
				}
        	
        	});
			
        }
        
        
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void alerta(){
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.alerta);
		builder.setMessage(R.string.msgSemUsuarioCadastrado);
		
		builder.setPositiveButton(R.string.bt_ok, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				
			}
		});
		
		alerta = builder.create();
		alerta.show();

	}
	
	public void btCadastrarUsuario(View v){
		auxiliar = true;
		Intent it = new Intent(this, CadastroUsuarioActivity.class);
		startActivity(it);
	}

	public static boolean getAuxiliar() {
		return auxiliar;
	}
	
	public static void setAuxiliar(boolean auxiliar) {
		ListaCartaoActivity.auxiliar = auxiliar;
	}
}

